﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using CoolCode.Entities;
using CoolCode.Services;
using CoolCode.Web;
using CoolCode.Linq;

namespace CoolCode.Web.UI.Views.Inquiry
{
    public partial class AdvanceInquiry : AuthPage
    {
        private IInspectionService _Service;
        public IInspectionService Service
        {
            get
            {
                if (_Service == null)
                {
                    _Service = ServiceFactory.Produce<IInspectionService>();
                }
                return _Service;
            }
        }

        private const int PageSize = 10;

        protected void Page_Load(object sender, EventArgs e)
        {
            handleEvents();

            if (!IsPostBack)
            {
                cdrCheckDateFr.Value = DateTime.Now.Date.AddMonths(-12);
                cdrCheckDateTo.Value = DateTime.Now.Date;
                productList.AutoLoad = false;
                //BindData_Master(new PageParam(0, PageSize));
                ddlStatus.Bind(Service.ListStatus(true));
            }
        }

        protected void handleEvents()
        {
            productList.PageIndexChanging += new GridViewPageEventHandler(pgrMaster_PageIndexChanging);
            lnkSearch.Click += new EventHandler(lnkSearch_Click);
        }

        protected void lnkSearch_Click(object sender, EventArgs e)
        {
            BindData_Master(new PageParam(0, PageSize));
            udpHeader.Update();
            udpSummary.Update();
        }

        private void BindData_Master(PageParam p)
        {
            var qb = QueryBuilder.Create<Inspection>()
                 .Fuzzy(c => c.ProductID, txtProductID.Text)
                 .Fuzzy(c => c.ProductSpec, txtProductSpec.Text)
                 .Between(c => c.CheckDate, cdrCheckDateFr.Value.GetValueOrDefault(), cdrCheckDateTo.Value.GetValueOrDefault())
                 .Equals(c => c.Status, ddlStatus.SelectedValue, ProductStatus.All.GetValue())
                 .Between(c => c.ODFar, rngODFar.TextFrom, rngODFar.TextTo)
                 .Between(c => c.ODClose, rngODClose.TextFrom, rngODClose.TextTo)
                 .Between(c => c.ConeHeight, rngConeHeight.TextFrom, rngConeHeight.TextTo)
                 .Between(c => c.SacculusLength, rngSacculusLength.TextFrom, rngSacculusLength.TextTo)
                 .Between(c => c.SacculusStretch, rngSacculusStretch.TextFrom, rngSacculusStretch.TextTo)
                 .Between(c => c.ODStressTest2, rngStressTest2.TextFrom, rngStressTest2.TextTo)
                 .Between(c => c.StressTestLength, rngStressTestLength.TextFrom, rngStressTestLength.TextTo);
            var list = Service.Paging(qb, p);
            productList.Model = list;
            productList.DataBind();

            //summary
            var qbGroup = QueryBuilder.Create<Inspection>()
                  .Fuzzy(c => c.ProductSpec, txtProductSpec.Text)
                  .Between(c => c.CheckDate, cdrCheckDateFr.Value.GetValueOrDefault(), cdrCheckDateTo.Value.GetValueOrDefault());
            var summary = Service.Summary(qb, qbGroup);
            productSummary.Model = summary;
            productSummary.DataBind();

        }

        private void BindData_Summary()
        {
            var qb = QueryBuilder.Create<Inspection>()
                  .Fuzzy(c => c.ProductSpec, txtProductSpec.Text)
                  .Between(c => c.CheckDate, cdrCheckDateFr.Value.GetValueOrDefault(), cdrCheckDateTo.Value.GetValueOrDefault());
            var summary = Service.ListSummaryBySpec(qb);
            productSummary.Model = summary;
            productSummary.DataBind();
        }

        protected void pgrMaster_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            BindData_Master(new PageParam(e.NewPageIndex, PageSize));
        }



    }

}
